package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public Result upload(List<String[]> list) {
        if (list == null || list.size() == 0) {
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        }
        try {
            for (String[] strings : list) {

                OrderSetting date1 = orderSettingDao.findByDate(strings[0]);
                System.out.println(date1);
                if (date1 == null) {
                    // 数据库中，今天没有设置预约
                    System.out.println(strings[0]);
                    orderSettingDao.insert(new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1])));
                } else {
                    orderSettingDao.updateByOrderDate(strings[0], Integer.parseInt(strings[1]));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    @Override
    // 根据年份和月份查询预约数据   ,格式： yyyy-MM
    public Result getOrderSettingByMonth(String date) {
        try {
            String startDate = date + "-1";
            String endDate = date + "-31";
            List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(startDate, endDate);
            List<Map> list = new ArrayList<>();
            if (orderSettingList != null && orderSettingList.size() > 0) {

                for (OrderSetting orderSetting : orderSettingList) {
                    Map<String, Object> m = new HashMap<>();
                    //获取几号
                    m.put("date", orderSetting.getOrderDate().getDate());
                    m.put("number", orderSetting.getNumber());
                    m.put("reservations", orderSetting.getReservations());
                    list.add(m);
                }
            }
            System.out.println(list);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }

    }

    @Override
    public Result  editNumberByDate(OrderSetting orderSetting){
        try {
            System.out.println(orderSetting.getOrderDate());
            System.out.println(orderSetting.getNumber());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            String date = sdf.format(orderSetting.getOrderDate());
            System.out.println(date);
            //查询有没有 该日期的预约
            OrderSetting orderSetting1 = orderSettingDao.findByDate(date);
            if(orderSetting1 == null) {
                // 没有 则插入
                orderSettingDao.insert(orderSetting);
            }else{
                //有则修改
                orderSettingDao.updateByOrderDate(date,orderSetting.getNumber());
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }

    //每日预约数
    @Override
    public List<Integer> findOrderCountByMonths(List<String> months) {
        List<Integer> orderCount = new ArrayList<>();
        for (String month : months) {
            String data = month;
            Integer count = orderSettingDao.findOrderCountByDate(data);
            orderCount.add(count);
        }
        return orderCount;
    }


}
